package com.duojuhe.admin.spring.webconfig;


import com.duojuhe.common.utils.token.TokenUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger文档配置
 *
 * @date 2018/2/9.
 */
@Profile("default")
@EnableSwagger2
@Configuration
public class SwaggerConfig {

    /** 是否开启swagger */
    @Value("${swagger.enabled}")
    private boolean enabled;

    @Bean
    public Docket admin_center() {
        String description = "<p>当前所有接口请求Body都是json格式</p>" +
                "<p>接口返回结果中errorCode（0=成功，其他一律弹窗提示message参数内容）</p>" +
                "<p>建议：当errorCode小于0 时请弹出提示后，重新定位到登录界面</p>" +
                "温馨提示：当接口文档中标识有参数的时候，如果所有参数都不填写的时候，请在body中传空json体{}，否则@RequestBody注解报异常";
        //添加header参数
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder()
                .name(TokenUtils.TOKEN_HEADER)
                .description("认证token，登录成功后获得的返回值")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("1")
                .required(true)
                .build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminCenter")
                .globalOperationParameters(parameters)
                .enable(enabled)
                .apiInfo(apiInfo("system系统模块接口", "1.0",description))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe"))
                .build().pathMapping("/");
    }


    @Bean
    public Docket basic_api() {
        //添加header参数
        List<Parameter> parameters = new ArrayList<>();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("basicApi")
                .globalOperationParameters(parameters)
                .enable(enabled)
                .apiInfo(apiInfo("开放无须登录接口", "1.0",""))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.chat"))
                .build().pathMapping("/");
    }

    private ApiInfo apiInfo(String name,String version,String description) {
        return new ApiInfoBuilder()
                .title(name)
                .version(version)
                .contact(new Contact("xxxx@qq.com", "", "xxxx@qq.com"))
                .description(description)
                .license("README.md")
                .licenseUrl("https://github.com")
                .build();
    }
}
